$(function () {

    var accountName = sessionStorage.getItem('accountName'),
        accountIsManager = sessionStorage.getItem('accountIsManager');

    //游客方式登录
    if(accountName === ''||accountName === null || accountName === undefined){
        //无法借阅书籍
        $(document).on('click','#borrow-book-btn',function () {
            confirm('请先登录！');
        })
    }



    var $search_input   = $('#search-input'),
        $auto_complete  = $('#auto-complete');

    //调用数据库呈现数据库中所有图书
    $.ajax({
        type:'GET',
        url :'editBook',
        data: {'type':1},
        success:function (data){
            console.log(data);
            var innerHTML = '';
            for( var i = 0;i<data.length;i++){
                innerHTML +='<tr class="book-data">\n' +
                    '           <td>'+data[i].bookId+'</td>\n' +
                    '           <td>'+data[i].bookName+'</td>\n' +
                    '           <td>'+data[i].bookAuthor+'</td>\n' +
                    '           <td>'+data[i].bookClass+'</td>\n' +
                    '           <td>'+data[i].bookPress+'</td>\n' +
                    '           <td>'+data[i].bookDescription+'</td>\n' +
                    '           <td>'+data[i].bookInventory+'</td>\n' +
                    '           <td class="btns-action">\n' +
                    '                <button class="action-btn" id="book-info-btn">详情</button>\n' +
                    '                <button class="action-btn" id="borrow-book-btn">借阅</button>\n' +
                    '            </td>\n' +
                    '         </tr>';
            }
            $('#book-table').append(innerHTML);
        },
        error:function (errorMsg) {
            console.log(errorMsg);
        }
    })

    //点击重置按钮
    $(document).on('click','#reset-btn',function () {
        console.log('点击重置按钮');
        $search_input.val('');
        $('tr.book-data').show();
    });

    //搜索框按键抬起和焦点聚焦自动补全
    $search_input.on('keyup focus',function () {
        var keyword = $(this).val();
        if(keyword !== '' && keyword !== null && keyword.length !== 0){
            $.ajax({
                type:'GET',
                url:'editBook',
                data:{'type':3,'keyword':keyword},
                success:function (data){
                    console.log(data);
                    var bookListHTML = '';
                    for(var i = 0;i<data.length;i++){
                        bookListHTML += '<li data-bookName="'+data[i].bookName+'">'+data[i].bookName+' : '+data[i].bookAuthor+'</li>';
                    }
                    $('#auto-complete-list').html(bookListHTML);
                    $auto_complete.show();
                },
                error:function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }else{
            $auto_complete.hide();
        }
    });


    $('.w').on('click','li',function (){
        $auto_complete.hide();
        var bookName = $(this).data('bookname'),
            $tr_book_data = $('tr.book-data');
        //在input框内补全书名
        $search_input.val(bookName);

        console.log(bookName);
        //刷新下方表格，筛出这本书
        $tr_book_data.hide().filter(":contains('"+bookName+"')").show();
    });


    //搜索框失去焦点隐藏自动补全框
    // $(document).on('blur','#search-input',function () {
    //     console.log('搜索框失去焦点');
    //     $auto_complete.hide();
    // })
    // $search_input.ready('blur',function () {
    //     console.log('搜索框失去焦点');
    //     $auto_complete.hide();
    // });

    //点击搜索按钮
    $('#search-btn').on('click',function(){
        $auto_complete.hide();
        var keyword =$search_input.val();
        console.log(keyword);
        //刷新下方表格，筛出这些书
        $.ajax({
            type:'GET',
            url :'editBook',
            data: {'type':3,'keyword':keyword},
            success:function (data){
                console.log(data);
                //清空表格
                $('.book-data').remove();
                //显示这些书
                var innerHTML = '';
                for(var i = 0;i<data.length;i++){
                    innerHTML +='<tr class="book-data">\n' +
                        '           <td>'+data[i].bookId+'</td>\n' +
                        '           <td>'+data[i].bookName+'</td>\n' +
                        '           <td>'+data[i].bookAuthor+'</td>\n' +
                        '           <td>'+data[i].bookClass+'</td>\n' +
                        '           <td>'+data[i].bookPress+'</td>\n' +
                        '           <td>'+data[i].bookDescription+'</td>\n' +
                        '           <td>'+data[i].bookInventory+'</td>\n' +
                        '           <td class="btns-action">\n' +
                        '                <button class="action-btn" id="edit-book">编辑</button>\n' +
                        '                <button class="action-btn" id="delete-book">删除</button>\n' +
                        '            </td>\n' +
                        '         </tr>';
                }

                $('#book-table').append(innerHTML);
            },
            error:function (errorMsg) {
                console.log(errorMsg);
            }
        })
    });

    //点击表格上的借阅按钮


    //点击表格上的详情按钮

})